package com.lovo.stock.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.stock.bean.*;
import com.lovo.stock.constant.MessageCode;
import com.lovo.stock.service.*;
import com.lovo.stock.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    IPurcartService purcartService;

    @Autowired
    IGoodService goodService;

    @Autowired
    IPurchaseService purchaseService;
    @Autowired
    IPurdetailService purdetailService;

    @Autowired
    IJournalService journalService;

    /**
     * 显示当前登录用户的购物车
     * @param request
     * @return
     */
    @RequestMapping("/queryPurcartByUserId")
    public ResponseResult<List<PurcartBean>> queryPurcartByUserId(HttpServletRequest request){
        ResponseResult<List<PurcartBean>> responseResult=new ResponseResult<>();
        try {
            UserBean userBean= (UserBean) request.getSession().getAttribute("user");
            responseResult.setData(purcartService.queryPurcartByUserId(userBean));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 根据商品编号查出商品
     * @param goodNum
     * @return
     */
    @RequestMapping("/queryGoodByGoodNum")
    public ResponseResult<GoodBean> queryGoodByGoodNum(String goodNum){
        ResponseResult<GoodBean> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(goodService.queryGoodByGoodNum(goodNum));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 添加商品到库和购物车
     */
    @RequestMapping("/addGood")
    @Transactional
    public ResponseResult addGood(HttpServletRequest request,@RequestBody GoodBean goodBean){
        ResponseResult responseResult=new ResponseResult<>();
        try {
            //添加商品信息到库存
            goodService.addGood(goodBean);
            //添加商品信息到购物车
            PurcartBean purcartBean=new PurcartBean();
            UserBean user= (UserBean) request.getSession().getAttribute("user");
            purcartBean.setPurcartUserId(user.getUserId());
            purcartBean.setPurcartGoodNum(goodBean.getGoodNum());
            purcartBean.setPurcartGoodName(goodBean.getGoodName());
            purcartBean.setPurcartBrand(goodBean.getGoodBrand());
            purcartService.addPurcart(purcartBean);
            return responseResult;
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
            return responseResult;
        }
    }

    /**
     * 将商品移出购物车
     * @param id
     */
    @RequestMapping("/deletePurcartById")
    public ResponseResult deletePurcartById(int id){
        ResponseResult responseResult=new ResponseResult<>();
        try {
            purcartService.deletePurcartById(id);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    @RequestMapping("/updatePucart")
    public ResponseResult updatePucart(@RequestBody PurcartBean purcartBean){
        ResponseResult responseResult=new ResponseResult<>();
        try {
            purcartService.updatePucart(purcartBean);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 入库
     * @return
     */
    @RequestMapping("/putInStorage")
    @Transactional
    public ResponseResult putInStorage(HttpServletRequest request, String purchase, String goods){
        PurchaseBean purchaseBean= JSONObject.parseObject(purchase,PurchaseBean.class);
        List<GoodBean> goodBeans=JSONObject.parseArray(goods,GoodBean.class);
        //获取入库时间
        purchaseBean.setPurchaseDate(LocalDate.now().toString());
        //设置详情商品类别
        for (int i = 0; i < goodBeans.size(); i++) {
            purchaseBean.getPurdetails().get(i).setPurdetailCategory((goodService.queryGoodByGoodNum(goodBeans.get(i).getGoodNum())).getGoodCategory());
        }

        ResponseResult responseResult=new ResponseResult<>();
        try {
            //生成采购详细信息
            purdetailService.addPurdetail(purchaseBean);
            //修改库中商品信息
            goodService.addGoods(goodBeans);
            //清空购物车
            purcartService.deletePurcartByUserId((UserBean) request.getSession().getAttribute("user"));
            //记录日志
            journalService.addJournal("进行了入库操作",(UserBean) request.getSession().getAttribute("user"));
            return responseResult;
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
            return responseResult;
        }
    }

    /**
     * 分页查询采购信息
     * @param pageInfo
     * @return
     */
    @RequestMapping("/queryPurchaseByPage")
    public ResponseResult<IPage<PurchaseBean>> queryPurchaseByPage(@RequestBody PageInfo<PurchaseBean> pageInfo){
        ResponseResult<IPage<PurchaseBean>> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(purchaseService.queryPurchaseByPage(pageInfo));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
       return responseResult;
    }

    /**
     * 查看采购详细信息
     * @param id
     * @return
     */
    @RequestMapping("/queryPurchaseAndDetailById")
    public ResponseResult<PurchaseBean> queryPurchaseAndDetailById(int id){
        ResponseResult<PurchaseBean> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(purchaseService.queryPurchaseAndDetailById(id));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }
}
